package com.example.sql_damo;

import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
  private DBhelpen dBhelpen;
  SQLiteDatabase database;
    private Button name;
    private Button pas;
    private  Button tz;
    private EditText tvname;
    private EditText tvpas;
    
  private int i=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.log);
        dBhelpen=new DBhelpen(this);
        name = findViewById(R.id.log);
        pas =  findViewById(R.id.registers);
        tvname = findViewById(R.id.user);
        tvpas = findViewById(R.id.pas);
        tz=findViewById(R.id.registerss);
        tz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,findpassword.class);
                startActivity(intent);
            }
        });
        pas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intents=new Intent(MainActivity.this,register.class);
                startActivity(intents);
            }
        });
        name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String names = tvname.getText().toString().trim();
                String pas = tvpas.getText().toString().trim();
//                if (i%3==0){
//                            dialogdomo s=new dialogdomo(MainActivity.this,"cssc");
//                            s.show();

 //               }
                if (login(names,pas)){
                    Toast.makeText(MainActivity.this, "成功   ", Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(MainActivity.this,Main2.class);
                    startActivity(intent);
                }else {
                   // i++;
                    if (pas.length()<8){
                    Toast.makeText(MainActivity.this, "密码小于八位数" , Toast.LENGTH_SHORT).show();
                }
                   if (names.equals("")){
                       Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                   }
                    Toast.makeText(MainActivity.this, "错误", Toast.LENGTH_SHORT).show();
                }





}
        });
    }
    public boolean login(String nameoo,String passwordoo){

        if (TextUtils.isEmpty(nameoo)){
            return false;
        }
        if (passwordoo.length()<8){
            return false;

        }
        database = dBhelpen.getReadableDatabase();

        Cursor cursor = database.query("INFOMATION",null,"NAME=? AND PAS=?",new String[]{nameoo+"",passwordoo+""},null,null,null);

        if (cursor.moveToNext()){
            cursor.close();
            database.close();
            return true;
        }
        return false;
    }


}
